﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using DomainModel;
using Services.Abstract;

namespace Web
{
    public partial class Rings : BasePage
    {
        public IRingService RingService { get; set; }
        public IPermissionService PermissionService { get; set; }

        protected new void Page_Load(object sender, EventArgs e)
        {
            SelectMenuItem("NavRings");

            if (!IsPostBack)
            {
                BindListViewRings();
            }

            InitAdditionalControls();
        }

        private void BindListViewRings()
        {
            var rings = RingService.GetAll().OrderBy(r => r.PairNo);
            var dataSource = new List<Object>();

            foreach (var ring in rings)
            {
                dataSource.Add(new
                {
                    Id = ring.Id,
                    PairNo = ring.PairNo,
                    BeginTime = ring.BeginTime.ToShortTimeString(),
                    EndTime = ring.EndTime.ToShortTimeString(),
                });
            }

            ListViewRings.DataSource = dataSource;
            ListViewRings.DataBind();
        }

        private int GetHour(string time)
        {
            int colonIndex = time.IndexOf(':');
            string hourStr = time.Substring(0, colonIndex);
            int hour = int.Parse(hourStr);
            return hour;
        }

        private int GetMinutes(string time)
        {
            int colonIndex = time.IndexOf(':');
            string minStr = time.Substring(colonIndex + 1, time.Length - colonIndex - 1);
            int minutes = int.Parse(minStr);
            return minutes;
        }

        protected void BtnChangeRings_OnClick(object sender, EventArgs e)
        {
            ShowEditRingsForm();

            var rings = RingService.GetAll().OrderBy(r => r.PairNo).ToList();

            int pairNo = 1;
            foreach (var ring in rings)
            {
                try
                {
                    string tbPairBeginName = "Tb" + (pairNo).ToString() + "PairBegin";
                    string tbPairEndName = "Tb" + (pairNo).ToString() + "PairEnd";

                    var tbPairBegin = (TextBox)EditRingsForm.FindControl(tbPairBeginName);
                    var tbPairEnd = (TextBox)EditRingsForm.FindControl(tbPairEndName);

                    tbPairBegin.Text = ring.BeginTime.ToShortTimeString();
                    tbPairEnd.Text = ring.EndTime.ToShortTimeString();

                    pairNo++;
                }
                catch (Exception ex)
                {
                    ShowSysMsg(ex.Message);
                    break;
                }

            }
        }

        protected void BtnSaveRings_OnClick(object sender, EventArgs e)
        {
            for (int i = 7; i >= 1; i--)
            {
                try
                {
                    var tbPairBegin = (TextBox)EditRingsForm.FindControl("Tb" + i.ToString() + "PairBegin");
                    var tbPairEnd = (TextBox)EditRingsForm.FindControl("Tb" + i.ToString() + "PairEnd");

                    string beginDateStr = tbPairBegin.Text;
                    string endDateStr = tbPairEnd.Text;

                    if (!string.IsNullOrEmpty(beginDateStr) && !string.IsNullOrEmpty(endDateStr))
                    {
                        DateTime beginTime = new DateTime(new TimeSpan(0, GetHour(tbPairBegin.Text), GetMinutes(tbPairBegin.Text), 0).Ticks);
                        DateTime endTime = new DateTime(new TimeSpan(0, GetHour(tbPairEnd.Text), GetMinutes(tbPairEnd.Text), 0).Ticks);

                        Ring ring = new Ring(i, beginTime, endTime);

                        RingService.SaveOrUpdate(ring);
                    }

                    HideEditRingsForm();

                    BindListViewRings();
                }
                catch (Exception ex)
                {
                    ShowFormError(EditRingsFormError.ClientID, ex.Message);
                }
            }
        }

        protected void BtnCancel_OnClick(object sender, EventArgs e)
        {
            HideEditRingsForm();
        }

        private void ShowEditRingsForm()
        {
            EditRingsForm.Visible = true;
            BtnChangeRings.Visible = false;
        }

        private void HideEditRingsForm()
        {
            EditRingsForm.Visible = false;
            BtnChangeRings.Visible = true;
        }
    }
}